package com.xhwl.logistics.converter.mapstruct;

import com.xhwl.logistics.bo.VehicleCarRecordBO;
import com.xhwl.logistics.bo.VehicleCarRecordInParkingLotBO;
import com.xhwl.logistics.bo.VehicleCarRecordRateBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarRecordInParkingLotBO;
import com.xhwl.logistics.dto.http.CarInParkingLotCheAnDTO;
import com.xhwl.logistics.dto.http.CarOccupiedInfoKeTopDTO;
import com.xhwl.logistics.dto.query.VehicleCarRecordInParkingLotDTO;
import com.xhwl.logistics.enums.VehicleCardTypeEnum;
import com.xhwl.logistics.util.AboutDateUtil;
import com.xhwl.logistics.vo.VehicleCarRecordInParkingLotVO;
import com.xhwl.logistics.vo.VehicleCarRecordRateVO;
import com.xhwl.logistics.vo.VehicleCarRecordVO;
import lombok.Getter;
import org.mapstruct.Mapper;
import org.mapstruct.NullValueMappingStrategy;

import java.util.ArrayList;
import java.util.List;

@Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface VehicleCarConverter {

    @Getter
    enum CheAnCardTypeEnum {

        TEMPORARY_CHEAN(0, "车安临时卡"),
        MONTH_CHEAN(26, "车安月卡"),
        VISITOR_CHEAN(28, "车安访客卡");

        CheAnCardTypeEnum(Integer id, String description) {
            this.id = id;
            this.description = description;
        }

        private final Integer id;

        private final String description;

        public static CheAnCardTypeEnum toCheAnCardTypeEnum(Integer id) {
            for (CheAnCardTypeEnum cheAnCardTypeEnum : CheAnCardTypeEnum.values()) {
                if (cheAnCardTypeEnum.id.equals(id)) {
                    return cheAnCardTypeEnum;
                }
            }
            return TEMPORARY_CHEAN;
        }

    }

    //*********************Query BO <- Controller DTO*********************
    QueryVehicleCarRecordInParkingLotBO vehicleCarRecordInParkingLotDTO2QueryVehicleCarRecordInParkingLotBO(VehicleCarRecordInParkingLotDTO vehicleCarRecordInParkingLotDTO);
    //*********************Query BO <- Controller DTO*********************


    //*********************VO <- BO*********************
    VehicleCarRecordInParkingLotVO vehicleCarRecordInParkingLotBO2VehicleCarRecordInParkingLotVO(VehicleCarRecordInParkingLotBO vehicleCarRecordInParkingLotBO);
    VehicleCarRecordRateVO vehicleCarRecordRateBO2VehicleCarRecordRateVO(VehicleCarRecordRateBO vehicleCarRecordRateBO);
    List<VehicleCarRecordVO> vehicleCarRecordBOList2VehicleCarRecordVOList(List<VehicleCarRecordBO> vehicleCarRecordBOList);
    //*********************VO <- BO*********************


    //*********carRecordInParkingLot CheAn*********
    //************BO <- Http DTO************
    default VehicleCarRecordInParkingLotBO toVehicleCarRecordInParkingLotBO(List<CarInParkingLotCheAnDTO> carInParkingLotCheAnDTOList, String parkingLotName) {
        VehicleCarRecordInParkingLotBO vehicleCarRecordInParkingLotBO = new VehicleCarRecordInParkingLotBO();

        vehicleCarRecordInParkingLotBO.setParkingLotName(parkingLotName);

        VehicleCarRecordRateBO vehicleCarRecordRateBO = new VehicleCarRecordRateBO(0, 0, 0);
        List<VehicleCarRecordBO> vehicleCarRecordBOList = new ArrayList<>();

        carInParkingLotCheAnDTOList.forEach(carInParkingLotCheAnDTO -> {
            VehicleCarRecordBO vehicleCarRecordBO = carInParkingLotCheAnDTO2VehicleCarRecordBO(carInParkingLotCheAnDTO);
            CheAnCardTypeEnum cheAnCardTypeEnum = CheAnCardTypeEnum.toCheAnCardTypeEnum(carInParkingLotCheAnDTO.getFunctype());
            switch (cheAnCardTypeEnum) {
                case TEMPORARY_CHEAN:
                    vehicleCarRecordBO.setCardType(VehicleCardTypeEnum.TEMPORARILY.name());
                    vehicleCarRecordRateBO.setTemporaryCar(vehicleCarRecordRateBO.getTemporaryCar() + 1);
                    break;
                case MONTH_CHEAN:
                    vehicleCarRecordBO.setCardType(VehicleCardTypeEnum.MONTH.name());
                    vehicleCarRecordRateBO.setMonthCar(vehicleCarRecordRateBO.getMonthCar() + 1);
                    break;
                case VISITOR_CHEAN:
                    vehicleCarRecordBO.setCardType(VehicleCardTypeEnum.VISITOR.name());
                    vehicleCarRecordRateBO.setVisitorCar(vehicleCarRecordRateBO.getVisitorCar() + 1);
            }
            vehicleCarRecordBOList.add(vehicleCarRecordBO);
        });

        vehicleCarRecordInParkingLotBO.setRate(vehicleCarRecordRateBO);
        vehicleCarRecordInParkingLotBO.setList(vehicleCarRecordBOList);
        return vehicleCarRecordInParkingLotBO;
    }
    default VehicleCarRecordBO carInParkingLotCheAnDTO2VehicleCarRecordBO(CarInParkingLotCheAnDTO carInParkingLotCheAnDTO) {
        VehicleCarRecordBO vehicleCarRecordBO = new VehicleCarRecordBO();
        vehicleCarRecordBO.setPlateNumber(carInParkingLotCheAnDTO.getCarno());
        vehicleCarRecordBO.setEnterTime(carInParkingLotCheAnDTO.getEntertime());
        vehicleCarRecordBO.setStayTime(AboutDateUtil.durationString(Integer.valueOf(carInParkingLotCheAnDTO.getParkingtime())));
        return vehicleCarRecordBO;
    }
    //************BO <- Http DTO************
    //*********carRecordInParkingLot CheAn*********

    //*********carRecordInParkingLot KeTop*********
    //************BO <- Http DTO************

//    default VehicleCarRecordInParkingLotBO toVehicleCarRecordInParkingLotBOByKeTop(List<CarInfoKeTopDTO> carInfoKeTopDTOList, String parkingLotName) {
//        VehicleCarRecordInParkingLotBO vehicleCarRecordInParkingLotBO = new VehicleCarRecordInParkingLotBO();
//        vehicleCarRecordInParkingLotBO.setParkingLotName(parkingLotName);
//
//        Map<Integer, Long> map = carInfoKeTopDTOList.stream()
//                .filter(carInfoKeTopDTO -> {
//                    String plateNo = carInfoKeTopDTO.getPlateNo();
//                    if (Strings.isNullOrEmpty(plateNo)) {
//                        return false;
//                    }
//                    return !"00000000".equals(plateNo);
//                })
//                .collect(Collectors.groupingBy(CarInfoKeTopDTO::getCarType, Collectors.counting()));
//
//        VehicleCarRecordRateBO vehicleCarRecordRateBO = new VehicleCarRecordRateBO(0, 0, 0);
//        map.forEach((key, value) -> {
//            if (Objects.nonNull(key)) {
//                VehicleCardTypeEnum vehicleCardTypeEnum = VehicleCardTypeEnum.toVehicleCardTypeEnumByKeTop(key);
//                switch (vehicleCardTypeEnum) {
//                    case FREE:
//                    case STORE:
//                    case VISITOR:
//                        break;
//                    case TEMPORARILY:
//                        vehicleCarRecordRateBO.setTemporaryCar(vehicleCarRecordRateBO.getTemporaryCar() + value.intValue());
//                        break;
//                    case MONTH:
//                        vehicleCarRecordRateBO.setMonthCar(vehicleCarRecordRateBO.getMonthCar() + value.intValue());
//                        break;
//                    default:
//                        throw new BusinessException("在场车辆查询（科拓）出现非免费卡，非月卡，非储值卡，非临时卡，非访客卡的情况");
//                }
//            }
//        });
//
//        vehicleCarRecordInParkingLotBO.setRate(vehicleCarRecordRateBO);
//        return vehicleCarRecordInParkingLotBO;
//    }

    default VehicleCarRecordInParkingLotBO toVehicleCarRecordInParkingLotBOByKeTop(List<CarOccupiedInfoKeTopDTO> carOccupiedInfoKeTopDTOList, String parkingLotName) {
        VehicleCarRecordInParkingLotBO vehicleCarRecordInParkingLotBO = new VehicleCarRecordInParkingLotBO();

        vehicleCarRecordInParkingLotBO.setParkingLotName(parkingLotName);

        VehicleCarRecordRateBO vehicleCarRecordRateBO = new VehicleCarRecordRateBO();
        carOccupiedInfoKeTopDTOList.forEach(carOccupiedInfoKeTopDTO -> {
            VehicleCardTypeEnum vehicleCardTypeEnum = VehicleCardTypeEnum.toVehicleCardTypeEnumByKeTop(carOccupiedInfoKeTopDTO.getCarType());
            Integer carNum = carOccupiedInfoKeTopDTO.getCarNum();
            switch (vehicleCardTypeEnum) {
                case TEMPORARILY:
                    vehicleCarRecordRateBO.setTemporaryCar(vehicleCarRecordRateBO.getTemporaryCar() + carNum);
                    break;
                case MONTH:
                    vehicleCarRecordRateBO.setMonthCar(vehicleCarRecordRateBO.getMonthCar() + carNum);
                    break;
                case STORE:
                    vehicleCarRecordRateBO.setStoredValueCard(vehicleCarRecordRateBO.getStoredValueCard() + carNum);
                    break;
                case FREE:
                    vehicleCarRecordRateBO.setFreeCard(vehicleCarRecordRateBO.getFreeCard() + carNum);
                    break;
                case VISITOR:
                    vehicleCarRecordRateBO.setVisitorCar(vehicleCarRecordRateBO.getVisitorCar() + carNum);
                    break;
                case VIP:
                    vehicleCarRecordRateBO.setVipCar(vehicleCarRecordRateBO.getVipCar() + carNum);
                    break;
                case HOUR_RENT:
                    vehicleCarRecordRateBO.setHourRentCar(vehicleCarRecordRateBO.getHourRentCar() + carNum);
                    break;
                case PROPERTY_RIGHT:
                    vehicleCarRecordRateBO.setPropertyRightCar(vehicleCarRecordRateBO.getPropertyRightCar() + carNum);
                    break;
                case COUNTING:
                    vehicleCarRecordRateBO.setCountingCar(vehicleCarRecordRateBO.getCountingCar() + carNum);
                    break;
                case HONORED_GUEST:
                    vehicleCarRecordRateBO.setHonoredGuestCard(vehicleCarRecordRateBO.getHonoredGuestCard() + carNum);
                    break;
                case EMPLOYEE:
                    vehicleCarRecordRateBO.setEmployeeCard(vehicleCarRecordRateBO.getEmployeeCard() + carNum);
                    break;
                default:
                    break;
            }
        });

        vehicleCarRecordInParkingLotBO.setRate(vehicleCarRecordRateBO);
        return vehicleCarRecordInParkingLotBO;
    }
    //************BO <- Http DTO************
    //*********carRecordInParkingLot KeTop*********

}
